package com.sfddata.musicpillow.user.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.sfddata.musicpillow.common.constant.BizCodeEnum;
import com.sfddata.musicpillow.common.respose.BaseRespose;
import com.sfddata.musicpillow.common.respose.EmptyObj;
import com.sfddata.musicpillow.user.feign.ThirdpartyFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sfddata.musicpillow.user.entity.QuestionInfoEntity;
import com.sfddata.musicpillow.user.service.QuestionInfoService;
import com.sfddata.musicpillow.common.utils.PageUtils;
import com.sfddata.musicpillow.common.utils.R;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * 
 *
 * @author jyo
 * @email 820389501@qq.com
 * @date 2022-08-12 10:40:24
 */
@RestController
@RequestMapping("user/question")
public class QuestionInfoController {
    @Autowired
    private QuestionInfoService questionInfoService;

    @Autowired
    private ThirdpartyFeignService thirdpartyFeignService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("user:questioninfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = questionInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("user:questioninfo:info")
    public R info(@PathVariable("id") Integer id){
		QuestionInfoEntity questionInfo = questionInfoService.getById(id);

        return R.ok().put("questionInfo", questionInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("user:questioninfo:save")
    public BaseRespose<EmptyObj> save(@RequestBody QuestionInfoEntity questionInfo){

        BaseRespose<EmptyObj> respose = new BaseRespose<>();

        try {
            questionInfoService.save(questionInfo);
        }catch (Exception e){
            respose.setCode(BizCodeEnum.ACCOUNT_NOTEXIST_ERROR.getCode());
            respose.setMsg(e.getMessage());
            return respose;
        }
        respose.setCode(BizCodeEnum.SUCCESS.getCode());
        respose.setMsg(BizCodeEnum.SUCCESS.getMsg());
        return respose;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("user:questioninfo:update")
    public R update(@RequestBody QuestionInfoEntity questionInfo){
		questionInfoService.updateById(questionInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("user:questioninfo:delete")
    public R delete(@RequestBody Integer[] ids){
		questionInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     *
     * 问题和反馈
     */
    @PostMapping("/addQAF")
    public BaseRespose<EmptyObj> addQAF(@RequestParam MultipartFile[] files,
                                        @Valid QuestionInfoEntity question) throws IOException {

        BaseRespose<EmptyObj> respose = new BaseRespose<>();
        List<String> fileList = new ArrayList<String>();
        for (MultipartFile file : files) {
            BaseRespose<String> UploadRespose = thirdpartyFeignService.uploadUserHeader(file);
            fileList.add(UploadRespose.getData());
        }
        try {
            questionInfoService.addQAF(question, fileList);
            respose.setCode(BizCodeEnum.SUCCESS.getCode());
            respose.setMsg(BizCodeEnum.SUCCESS.getMsg());
            return respose;
        }catch (Exception e){
            respose.setCode(BizCodeEnum.UNKNOW_EXCEPTION.getCode());
            respose.setMsg(e.getMessage());
            return respose;
        }
    }

    @GetMapping("/helpPhone")
    public BaseRespose<String> helpPhone(){
        BaseRespose<String> respose = new BaseRespose<>();
        respose.setData("15056574789");
        respose.setCode(BizCodeEnum.SUCCESS.getCode());
        respose.setMsg(BizCodeEnum.SUCCESS.getMsg());
        return respose;
    }

    @GetMapping("/helpQuestion")
    public BaseRespose<List<QuestionInfoEntity>> helpQuesion(){

        List<QuestionInfoEntity> list = new ArrayList<>();

        QuestionInfoEntity question100 = questionInfoService.getById(100);
        list.add(question100);
        list.add(question100);

        BaseRespose<List<QuestionInfoEntity>> respose = new BaseRespose<>();
        respose.setData(list);
        respose.setCode(BizCodeEnum.SUCCESS.getCode());
        respose.setMsg(BizCodeEnum.SUCCESS.getMsg());
        return respose;
    }

}
